package game.module.battleReport.processor;

import java.util.List;

import db.proto.ProtoMessageQzmj.ARound;
import db.proto.ProtoMessageQzmj.AllRound;
import game.HallServer;
import game.common.PlayingRoleMsgProcessor;
import game.entity.PlayingRole;
import game.module.battleReport.bean.BattleReport;
import game.module.battleReport.dao.BattleReportCache;
import lion.common.MsgCodeAnn;
import lion.netty4.message.MyRequestMessage;
import lion.netty4.message.RequestMessageRaw;
import lion.netty4.message.RequestProtoMessage;
import lua.LuaMessageHall.BattleAround;
import lua.LuaMessageHall.C2SGetReport;
import lua.LuaMessageHall.S2CGetReport;
import lua.LuaMessageLogin.S2ErrorCode;

@MsgCodeAnn(msgcode = C2SGetReport.id, accessLimit = 500)
public class GetAroundbattle extends PlayingRoleMsgProcessor {

	@Override
	public void process(PlayingRole playingRole, RequestMessageRaw requestMessage) throws Exception {
		// TODO Auto-generated method stub
		int playerId = playingRole.getId();
		C2SGetReport reqMsg = C2SGetReport.parse(requestMessage);
		long reportid = reqMsg.reportid;
		HallServer.executorService.execute(new Runnable() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				BattleReportCache.getInstance().addBattleReportInCache(reportid, playerId);
				BattleReport battlereport = BattleReportCache.getInstance().getRealReport(reportid);
				if(battlereport==null) {
					S2ErrorCode respMsg = new S2ErrorCode(136);
					playingRole.writeAndFlush(respMsg.build(playingRole.alloc()));
					return;
				}
				S2CGetReport respMsg = new S2CGetReport();
				AllRound  allround = battlereport.getAllround();
				int size = allround.getRoundsCount();
				List<ARound> rounds = allround.getRoundsList();
				respMsg.around = new BattleAround[size];
				for(int i =0 ;i<size;i++) {
					ARound around = rounds.get(i);
					respMsg.around[i] = new BattleAround();
					List<Integer> score = around.getASocreList();
					respMsg.around[i].score =new int[score.size()];
					int j = 0;
					for(Integer aScore:score) {
						respMsg.around[i].score[j++] = aScore;
					}
					respMsg.around[i].time = around.getCreatetime();
				}
				playingRole.writeAndFlush(respMsg.build(playingRole.alloc()));
			}
		});
		
	}

	@Override
	public void processProto(PlayingRole playingRole, RequestProtoMessage request) throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void processWebsocket(PlayingRole playingRole, MyRequestMessage request) throws Exception {
		// TODO Auto-generated method stub
		
	}

}
